package com.hippo.core.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.hippo.core.ui.theme.HippoTheme

@Composable
fun BottomDialog(
    title:String,
    onDismissRequest: ()->Unit,
    modifier: Modifier = Modifier,
    dialogProperties:DialogProperties = DialogProperties(),
    content: @Composable ColumnScope.() -> Unit
) {

    Dialog(
        onDismissRequest = onDismissRequest,
        properties = dialogProperties,
    ){

        Box(
            contentAlignment = Alignment.Center,
            modifier = modifier
                .fillMaxSize()
        ){


            Column(
                modifier = modifier
                    .fillMaxWidth()
                    .background(
                        color = Color(0xB2B2B2),
                        shape = RoundedCornerShape(8.dp,)
                    )
            ) {

                Box(
                    modifier = modifier
                        .fillMaxWidth()
                        .background(
                            color = Color.LightGray,
                            shape = RoundedCornerShape(topStart = 8.dp, topEnd = 8.dp)
                        )
                ){

                    Text(
                        text = title,
                        modifier = modifier.align(Alignment.Center)
                    )

                    IconButton(
                        modifier = modifier.align(Alignment.CenterEnd),
                        onClick = onDismissRequest,
                    ) {
                        Icon(
                            imageVector = Icons.Default.Close,
                            contentDescription = "Close",
                            tint = MaterialTheme.colorScheme.primary
                        )
                    }

                }

                HorizontalDivider(
                    thickness = 0.5.dp,
                    color = MaterialTheme.colorScheme.outlineVariant
                )

                Column(
                    modifier = modifier
                        .fillMaxWidth()
                        .background(
                            color = Color.White,
                            shape = RoundedCornerShape(bottomStart = 8.dp, bottomEnd = 8.dp)
                        )
                        .padding(16.dp)
                ) {

                    content()

                }

            }
        }

    }

}


@Preview
@Composable
private fun CommonDialogPreview() {

    HippoTheme {
        BottomDialog(
            title = "标题",
            onDismissRequest = {}
        ){
            Text(text = "内容")
        }
    }

}